package ace.tool.generate.code.impl.manager.core;

import ace.tool.generate.code.impl.generator.DefaultGenerator;
import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.GenerateContext;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.TestGraphqlTestFileBundle;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestAddFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestGetFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestListFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestModifyFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestRemoveFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestRemoveLogicFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestSaveFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.test.graphql.TestGraphqlTestSearchFileInfo;
import java.util.HashMap;
import java.util.Map;
import lombok.SneakyThrows;

/**
 * @author caspar
 * @date 2024/1/11 10:12 core project manager generator
 */
public class CoreProjectTestResourceGraphqlTestFilesManager {

  private final GenerateContext context;
  private final EntityModelManager entityModelManager = new EntityModelManager();

  public CoreProjectTestResourceGraphqlTestFilesManager(GenerateContext context) {
    this.context = context;
  }

  @SneakyThrows
  public void generate() {
    context
        .getModuleProject()
        .getCoreProject()
        .getTestGraphqlTestFileBundles()
        .forEach(this::generateTestResourceGraphqlTestFile);
  }

  private void generateTestResourceGraphqlTestFile(
      TestGraphqlTestFileBundle testGraphqlTestFileBundle) {
    this.generateTestResourceGraphqlTestGetFile(
        testGraphqlTestFileBundle.getTestGraphqlTestGetFileInfo());

    this.generateTestResourceGraphqlTestAddFile(
        testGraphqlTestFileBundle.getTestGraphqlTestAddFileInfo());

    this.generateTestResourceGraphqlTestListFile(
        testGraphqlTestFileBundle.getTestGraphqlTestListFileInfo());

    this.generateTestResourceGraphqlTestModifyFile(
        testGraphqlTestFileBundle.getTestGraphqlTestModifyFileInfo());

    this.generateTestResourceGraphqlTestSaveFile(
        testGraphqlTestFileBundle.getTestGraphqlTestSaveFileInfo());

    this.generateTestResourceGraphqlTestRemoveFile(
        testGraphqlTestFileBundle.getTestGraphqlTestRemoveFileInfo());

    this.generateTestResourceGraphqlTestRemoveLogicFile(
        testGraphqlTestFileBundle.getTestGraphqlTestRemoveLogicFileInfo());

    this.generateTestResourceGraphqlTestSearchFile(
        testGraphqlTestFileBundle.getTestGraphqlTestSearchFileInfo());
  }

  private void generateTestResourceGraphqlTestSaveFile(
      TestGraphqlTestSaveFileInfo testGraphqlTestSaveFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestSaveFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestSaveFileInfo.getInfo().getMethodName());
    model.put("inputType", testGraphqlTestSaveFileInfo.getInfo().getInputType());
    model.put("fields", testGraphqlTestSaveFileInfo.getInfo().getFields());

    String templateFile = "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestSaveFileInfo.ftl";
    String outputFileFullName = testGraphqlTestSaveFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestSearchFile(
      TestGraphqlTestSearchFileInfo testGraphqlTestSearchFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestSearchFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestSearchFileInfo.getInfo().getMethodName());
    model.put("fields", testGraphqlTestSearchFileInfo.getInfo().getFields());
    model.put("dtoType", testGraphqlTestSearchFileInfo.getInfo().getDtoType());

    String templateFile =
        "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestSearchFileInfo.ftl";
    String outputFileFullName = testGraphqlTestSearchFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestRemoveLogicFile(
      TestGraphqlTestRemoveLogicFileInfo testGraphqlTestRemoveLogicFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestRemoveLogicFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestRemoveLogicFileInfo.getInfo().getMethodName());
    model.put("fields", testGraphqlTestRemoveLogicFileInfo.getInfo().getFields());

    String templateFile =
        "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestRemoveLogicFileInfo.ftl";
    String outputFileFullName = testGraphqlTestRemoveLogicFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestRemoveFile(
      TestGraphqlTestRemoveFileInfo testGraphqlTestRemoveFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestRemoveFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestRemoveFileInfo.getInfo().getMethodName());
    model.put("fields", testGraphqlTestRemoveFileInfo.getInfo().getFields());

    String templateFile =
        "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestRemoveFileInfo.ftl";
    String outputFileFullName = testGraphqlTestRemoveFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestModifyFile(
      TestGraphqlTestModifyFileInfo testGraphqlTestModifyFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestModifyFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestModifyFileInfo.getInfo().getMethodName());
    model.put("inputType", testGraphqlTestModifyFileInfo.getInfo().getInputType());
    model.put("fields", testGraphqlTestModifyFileInfo.getInfo().getFields());

    String templateFile =
        "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestModifyFileInfo.ftl";
    String outputFileFullName = testGraphqlTestModifyFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestAddFile(
      TestGraphqlTestAddFileInfo testGraphqlTestAddFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("inputType", testGraphqlTestAddFileInfo.getInfo().getInputType());
    model.put("methodName", testGraphqlTestAddFileInfo.getInfo().getMethodName());

    String templateFile = "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestAddFileInfo.ftl";
    String outputFileFullName = testGraphqlTestAddFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestGetFile(
      TestGraphqlTestGetFileInfo testGraphqlTestGetFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestGetFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestGetFileInfo.getInfo().getMethodName());
    model.put("fields", testGraphqlTestGetFileInfo.getInfo().getFields());

    String templateFile = "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestGetFileInfo.ftl";
    String outputFileFullName = testGraphqlTestGetFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }

  private void generateTestResourceGraphqlTestListFile(
      TestGraphqlTestListFileInfo testGraphqlTestListFileInfo) {
    Map<String, Object> model = new HashMap<>();
    model.put("idType", testGraphqlTestListFileInfo.getInfo().getIdType());
    model.put("methodName", testGraphqlTestListFileInfo.getInfo().getMethodName());
    model.put("fields", testGraphqlTestListFileInfo.getInfo().getFields());

    String templateFile =
        "/schema/core-project/test/resources/graphql-test/CoreProjectTestResourceGraphqlTestGetListFileInfo.ftl";
    String outputFileFullName = testGraphqlTestListFileInfo.getInfo().getFileFullName();
    new DefaultGenerator(templateFile, outputFileFullName).generate(model);
  }
}
